package com.zw.pdm.module.invention.service;

import com.zw.pdm.entity.invention.PttInvent;
import com.zw.pdm.module.craft.mapper.CraftReceiveMapper;
import com.zw.pdm.module.craft.mapper.EmployeeQueryMapper;
import com.zw.pdm.module.craft.model.ComprehensiveQueryModel;
import com.zw.pdm.module.craft.repository.CraftMakeDetailRepository;
import com.zw.pdm.module.invention.mapper.InventionMapper;
import com.zw.pdm.module.invention.repository.PttInventRepository;
import com.zw.pdm.module.report.mapper.ChangeMapper;
import com.zw.pdm.module.report.mapper.PlanMapper;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.CommonUtils;
import com.zw.pdm.util.FileUtils;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * @Description:
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class InventionService {
    @Autowired
    private PttInventRepository pttInventRepository;
    @Autowired
    private InventionMapper inventionMapper;
    @Autowired
    private LogService logService;
    @Value("${user.upload.path}")
    private String uploadPath;

    /**
     * 查询专利表
     *
     * @param pttInvent
     * @return
     */

    public List<PttInvent> selectQuery(PttInvent pttInvent) {
        return inventionMapper.selectQuery(pttInvent);
    }

    /**
     * 保存 添加的专利
     *
     * @param pttInvent
     * @return
     */
    public int savePttInvent(PttInvent pttInvent) {
        pttInvent.setCreateMan(ShiroUtils.getUser().getName());
        pttInvent.setCreateTime(new Date());
        pttInvent.setIsDel(0);
        PttInvent pttInvent1 = pttInventRepository.save(pttInvent);
        logService.saveLog("发明专利信息保存", "保存表ID-专利名称：" + pttInvent1.getId()+"-"+pttInvent1.getName());

        return 1;
    }

    public int del(PttInvent pttInvent) {
        PttInvent pttInvent1= pttInventRepository.getOne(pttInvent.getId());
        pttInvent1.setIsDel(1);
        pttInventRepository.save(pttInvent1);
        logService.saveLog("发明专利信息删除", "删除ID：" + pttInvent1.getId());
        return 2;
    }

    /**
     * 维护专利信息
     * @param pttInvent
     * @return
     */
    public int editPttInvent(PttInvent pttInvent) {
        PttInvent pttInvent1= pttInventRepository.getOne(pttInvent.getId());
        pttInvent.setLastMan(ShiroUtils.getUser().getName());
        pttInvent.setLastTime(new Date());
        pttInvent.setCreateMan(pttInvent1.getCreateMan());
        pttInvent.setCreateTime(pttInvent1.getCreateTime());
        pttInvent.setIsDel(pttInvent1.getIsDel());
        pttInvent.setCertificateNo(pttInvent1.getCertificateNo());
        pttInventRepository.save(pttInvent);
        logService.saveLog("发明专利信息维护", "维护ID：" + pttInvent.getId());
        return 2;
    }

    public int upload(PttInvent pttInvent, MultipartFile file) {
        if (null != file) {
            StringBuffer sb = new StringBuffer();
            sb.append(uploadPath);
            sb.append(pttInvent.getId());
            sb.append("/");
            String fileName = FileUtils.upload(file, sb.toString());
            if (CommonUtils.isNotNull(fileName)) {
                logService.saveLog("专利上传附件", "专利ID-附件名称："+pttInvent.getId()+"-" + fileName);
                PttInvent pttInvent1 = pttInventRepository.getOne(pttInvent.getId());
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("/files/").append(pttInvent1.getId()).append("/");
                pttInvent1.setCertificateNo(stringBuffer + fileName);
                pttInventRepository.save(pttInvent1);
                return 1;
            }
        }
        return -1;
    }

}
